package com.aplink.generic.util;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;

public class CityAsyncTask extends AsyncTask<String, String, StringBuilder> {
    Activity act;
    double latitude;
    double longitude;

    public CityAsyncTask(final Activity act, final double latitude,
            final double longitude) {
        // TODO Auto-generated constructor stub
        this.act = act;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    @Override
    protected StringBuilder doInBackground(final String... arg0) {
        final StringBuilder _address = new StringBuilder();
        final Geocoder geocoder = new Geocoder(this.act, Locale.getDefault());
        try {
            final List<Address> addresses = geocoder.getFromLocation(
                    this.latitude, this.longitude, 1);
            Address address = null;
            address = addresses.get(0);
            _address.append("Address: ").append(address.getAddressLine(0))
                    .append("\n");
            _address.append("City: ").append(address.getLocality())
                    .append("\n");
            _address.append("Sub-Admin Area: ")
                    .append(address.getSubAdminArea()).append("\n");
            _address.append("Country: ").append(address.getCountryName())
                    .append("\n");

        } catch (final IOException e) {
            e.printStackTrace();
        }
        return _address;
    }

    @Override
    protected void onPostExecute(final StringBuilder result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }

}
